﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Quartz.WebLib;

namespace ZX.WebApp.Quartz
{
    public partial class quartz : System.Web.UI.Page
    {
        static QuartzDataHandler quartzDataHandler = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            string flag = this.Request.Params["flag"];
            string json = string.Empty;
            if (quartzDataHandler == null)
            {
                quartzDataHandler = QuartzDataHandler.GetInstance();
            }
            switch (flag)
            {
                case "Init": //初始化QuartzDataHandler
                    if (quartzDataHandler == null)
                    {
                        try
                        {
                            quartzDataHandler = QuartzDataHandler.GetInstance();
                            if (quartzDataHandler != null)
                            {
                                json = "{\"IsError\":false,\"Message\":\"初始化成功！\",\"Data\":null}";
                            }
                            else
                            {
                                json = "{\"IsError\":false,\"Message\":\"初始化失败！\",\"Data\":null}";
                            }
                        }
                        catch
                        {
                            json = "{\"IsError\":false,\"Message\":\"初始化失败！\",\"Data\":null}";
                        }
                    }
                    break;
                case "GetSchedulerInfo": //获取调度程序信息
                    System.Text.StringBuilder sb = new System.Text.StringBuilder("{");
                    sb.Append("\"SchedulerName\":\"" + quartzDataHandler.SchedulerName + "\",");
                    sb.Append("\"SchedulerInstanceID\":\"" + quartzDataHandler.SchedulerInstanceID + "\",");
                    sb.Append("\"SchedulerIsRemote\":" + quartzDataHandler.SchedulerIsRemote.ToString().ToLower() + ",");
                    sb.Append("\"SchedulerType\":\"" + quartzDataHandler.SchedulerType + "\",");
                    sb.Append("\"SchedulerStartTime\":\"" + quartzDataHandler.SchedulerStartTime.ToString("yyyy-MM-dd hh:mm:ss") + "\",");
                    sb.Append("\"SchedulerRunningStatus\":\"" + quartzDataHandler.SchedulerRunningStatus + "\",");
                    sb.Append("\"JobTotal\":" + quartzDataHandler.JobTotal + ",");
                    sb.Append("\"JobExecuteTotal\":" + quartzDataHandler.JobExecuteTotal + "");
                    sb.Append("}");
                    json = sb.ToString();
                    break;
                case "GetJobDetailInfo": //获取任务详细信息
                    json = quartzDataHandler.GetJobDetailsData("localJob", "default");
                    break;
                case "GetJobLists": //获取所有任务数据
                    json = quartzDataHandler.GetAllJobJSON();
                    break;
                case "GetJobGroupNameLists": //获取所有任务组名称
                    json = quartzDataHandler.GetAllGroupsNameJSON();
                    break;
                case "GetTriggerGroupNameLists": //获取所有计划组名称
                    json = quartzDataHandler.GetAllTriggerGroupNameJSON();
                    break;
                case "AddJob": //添加任务
                    string jobInfo = this.Request.Params["jobInfo"];
                    json = quartzDataHandler.AddJob(jobInfo);
                    break;
                case "DeleteJob": //删除任务
                    string jobNameDelete = this.Request.Params["jobName"];
                    string jobGroupNameDelete = this.Request.Params["jobGroupName"];
                    if (quartzDataHandler.DeleteJob(jobNameDelete, jobGroupNameDelete))
                    {
                        json = "{\"IsError\":false,\"Message\":\"任务删除成功！\",\"Data\":null}";
                    }
                    else
                    {
                        json = "{\"IsError\":true,\"Message\":\"任务删除失败！\",\"Data\":null}";
                    }
                    break;
                case "ExecuteJob": //立即执行任务
                    string jobNameExecute = this.Request.Params["jobName"];
                    string jobGroupNameExecute = this.Request.Params["jobGroupName"];
                    if (quartzDataHandler.RunJobNow(jobNameExecute, jobGroupNameExecute))
                    {
                        json = "{\"IsError\":false,\"Message\":\"任务执行成功！\",\"Data\":null}";
                    }
                    else
                    {
                        json = "{\"IsError\":true,\"Message\":\"任务执行失败！\",\"Data\":null}";
                    }
                    break;
                case "PauseJob": //暂停任务
                    string jobNamePause = this.Request.Params["jobName"];
                    string jobGroupNamePause = this.Request.Params["jobGroupName"];
                    if (quartzDataHandler.PauseJob(jobNamePause, jobGroupNamePause))
                    {
                        json = "{\"IsError\":false,\"Message\":\"任务暂停成功！\",\"Data\":null}";
                    }
                    else
                    {
                        json = "{\"IsError\":true,\"Message\":\"任务暂停失败！\",\"Data\":null}";
                    }
                    break;
                case "ResumeJob": //恢复任务
                    string jobNameResume = this.Request.Params["jobName"];
                    string jobGroupNameResume = this.Request.Params["jobGroupName"];
                    if (quartzDataHandler.ResumeJob(jobNameResume, jobGroupNameResume))
                    {
                        json = "{\"IsError\":false,\"Message\":\"任务恢复成功！\",\"Data\":null}";
                    }
                    else
                    {
                        json = "{\"IsError\":true,\"Message\":\"任务恢复失败！\",\"Data\":null}";
                    }
                    break;
                case "GetJobPlanInfo": //获取任务计划信息
                    string jobName = this.Request.Params["jobName"];
                    string jobGroupName = this.Request.Params["jobGroupName"];
                    string op = this.Request.Params["op"];
                    json = quartzDataHandler.GetJobPlanInfoJSON(jobName, jobGroupName, op);
                    this.Response.ContentType = "application/json";
                    break;
                case "UploadJobDll":
                    HttpFileCollection files = HttpContext.Current.Request.Files;
                    if (files.Count > 0)
                    {
                        json = UpLoadFile(files[0]);
                    }
                    else
                    {
                        json = "<script>parent.fileUploadCallBack(0, '没有可上传的文件！');</script>";
                    }
                    break;
            }
            this.Response.Write(json);
            this.Response.End();
        }

        //上传文件处理方法
        private string UpLoadFile(HttpPostedFile pf)
        {
            string FileName = System.IO.Path.GetFileName(pf.FileName);
            string FileExtention = System.IO.Path.GetExtension(pf.FileName);
            if (FileName != "")
            {
                if (pf.ContentLength > 1024 * 1024)
                {
                    return "<script>parent.fileUploadCallBack(0, '抱歉上传文件太大，目前支持1M文件！');</script>";
                }
                else
                {
                    string path = AppDomain.CurrentDomain.BaseDirectory + "bin\\" + "Service";//System.Web.HttpContext.Current.Request.MapPath("~/UpFile");
                    if (!System.IO.Directory.Exists(path))
                    {
                        System.IO.Directory.CreateDirectory(path);
                    }
                    else
                    {
                        pf.SaveAs(path + "\\" + FileName);

                    }
                    if (System.IO.File.Exists(path + "\\" + FileName))
                    {
                        return "<script>parent.fileUploadCallBack(0, '上传成功！');</script>";
                    }
                    else
                    {
                        return "<script>parent.fileUploadCallBack(1, '上传失败！');</script>";
                    }
                }
            }
            else
            {
                return "<script>parent.fileUploadCallBack(2, '未找到上传文件！');</script>";
            }
        }
    }
}